﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using MVVM.Core;

namespace ObjectModel.Excel
{
    /// <summary>
    /// A bindable wrapper to the selected range in an Excel app.
    /// </summary>
    public class SelectedRange : NotificationBase
    {
        Application mApp = Globals.Application;
        Range mSelectedRange;

        public SelectedRange()
        {
            mSelectedRange = mApp.Selection as Range;

            mApp.SheetSelectionChange += SheetSelectionChange;
        }

        void SheetSelectionChange(object Sh, Range Target)
        {
            mSelectedRange = Target;

            OnPropertyChanged("AddressExternal");
        }

        public string AddressExternal
        {
            get
            {
                return mSelectedRange.GetExternalAddress();
            }
        }

        void RunWithoutEvents(System.Action ac)
        {
            mApp.SheetSelectionChange -= SheetSelectionChange;
            ac();
            mApp.SheetSelectionChange += SheetSelectionChange;
        }
    }
}
